package com.yisoo.ubag.product.service.impl;

import com.yisoo.ubag.product.entity.DevTokenEntity;
import com.yisoo.ubag.product.service.DevTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.product.dao.DevSecBraConDao;
import com.yisoo.ubag.product.entity.DevSecBraConEntity;
import com.yisoo.ubag.product.service.DevSecBraConService;


@Service("devSecBraConService")
public class DevSecBraConServiceImpl extends ServiceImpl<DevSecBraConDao, DevSecBraConEntity> implements DevSecBraConService {


    @Autowired
    DevTokenService devTokenService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DevSecBraConEntity> page = this.page(
                new Query<DevSecBraConEntity>().getPage(params),
                new QueryWrapper<DevSecBraConEntity>()
        );

        return new PageUtils(page);
    }


    //还没有绑定到dev的token
    @Override
    public List<DevTokenEntity> getResidue(DevTokenEntity devSecBraCon) {
        QueryWrapper<DevSecBraConEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("consumer_id",devSecBraCon.getMasterId())
                .eq("brand_id",devSecBraCon.getBrandId())
                .eq("scene_id",devSecBraCon.getSceneId());
        List<DevSecBraConEntity> devSecBraConEntities = baseMapper.selectList(queryWrapper);
        List<DevTokenEntity> devTokenEntities = devTokenService.getCountByCidBidSid(devSecBraCon.getMasterId(),devSecBraCon.getBrandId(),devSecBraCon.getSceneId());

        List<DevTokenEntity> collect = devTokenEntities.stream().filter(temp->{
            if (temp.getTokenUsing() != devSecBraCon.getTokenUsing()) return false;
            for (DevSecBraConEntity c:devSecBraConEntities){
                if (c.getRunToken().equals(temp.getRunToken())){
                    return false;
                }
            }
            return true;
        }).collect(Collectors.toList());
        return  collect;
    }

    @Override
    public DevSecBraConEntity getDataById(Long devid) {
        QueryWrapper<DevSecBraConEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dev_id",devid);
        return baseMapper.selectOne(queryWrapper);
    }

}